package kafkastudy.rocketmq.simple;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import java.util.concurrent.CountDownLatch;

/**
 * Created by hp on 2019/9/25.
 */
public class AsyncProducer {

    public static void main(String[] args) throws Exception {
        DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name2");
        producer.setNamesrvAddr("10.1.1.152:9876");
        producer.start();
        producer.setRetryTimesWhenSendAsyncFailed(0);
        //for(int i=0;i<100;i++) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
            Message msg = new Message("TopicTest2",
                    "TagA",
                    "OrderID188",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.send(msg, new SendCallback() {

                @Override
                public void onSuccess(SendResult sendResult) {
                    countDownLatch.countDown();
                    System.out.printf("%-10d OK %s %n",
                            sendResult.getMsgId());
                }
                @Override
                public void onException(Throwable e) {
                    countDownLatch.countDown();
                    System.out.printf("%-10d Exception %s %n", e);
                    e.printStackTrace();
                }
            });

        //}
        countDownLatch.await();
        producer.shutdown();

    }
}
